//
//  HZLandscapeWuDangView.m
//  Stock_Health
//
//  Created by MayerF on 2017/8/2.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZLandscapeWuDangView.h"

@interface HZLandscapeWuDangView()
@property (nonatomic, strong)UIView *pVerticalLine;
@property (nonatomic, strong)NSMutableArray *pBuySellArray;
@property (nonatomic, strong)NSMutableArray *pPriceArray;
@property (nonatomic, strong)NSMutableArray *pNumArray;
@property (nonatomic, assign)NSInteger nMaxNum;
@property (nonatomic, strong)NSArray<HZWuDangModel *> *pModelArray;
@end

@implementation HZLandscapeWuDangView
- (NSMutableArray *)pBuySellArray
{
    if (!_pBuySellArray) {
        _pBuySellArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pBuySellArray;
}
- (NSMutableArray *)pPriceArray
{
    if (!_pPriceArray) {
        _pPriceArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pPriceArray;
}
- (NSMutableArray *)pNumArray
{
    if (!_pNumArray) {
        _pNumArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pNumArray;
}

#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = [HZCommonTool getThemeColor];
    }
    return self;
}

- (void)layoutSubviews{
    _pVerticalLine.center = CGPointMake(CGRectGetWidth(self.frame)/2, CGRectGetHeight(self.frame)/2);
    _pVerticalLine.bounds = CGRectMake(0, 0, CGRectGetWidth(self.frame), 0.5);
    
    CGFloat fItemW = (CGRectGetWidth(self.frame) - 30)/2;
    CGFloat fItemH = CGRectGetHeight(self.frame)/10;
    for (int i = 0; i < _pPriceArray.count; i++) {
        UILabel *pBuySell = _pBuySellArray[i];
        UILabel *pPrice = _pPriceArray[i];
        UILabel *pNum = _pNumArray[i];
        CGFloat Y = fItemH * i;
        CGFloat priceX = 1;
        pBuySell.frame = CGRectMake(priceX, Y, 30, fItemH);
        pPrice.frame = CGRectMake(CGRectGetMaxX(pBuySell.frame), Y, fItemW + 5, fItemH);
        pNum.frame = CGRectMake(CGRectGetMaxX(pPrice.frame), Y, fItemW-5, fItemH);
    }
}
#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _nMaxNum = 0;
    _pVerticalLine = [UIView new];
    _pVerticalLine.backgroundColor = kMYHexToRGB(0xf60300, 1);
    [self addSubview:_pVerticalLine];
    
    NSArray *pTitleArray = [NSArray arrayWithObjects:
                            kHZSell5,
                            kHZSell4,
                            kHZSell3,
                            kHZSell2,
                            kHZSell1,
                            kHZBuy1,
                            kHZBuy2,
                            kHZBuy3,
                            kHZBuy4,
                            kHZBuy5, nil];
    for (NSString *title in pTitleArray) {
        UILabel *pBuySell = [UILabel new];
        UILabel *pPrice = [UILabel new];
        UILabel *pNum = [UILabel new];
        pBuySell.text = title;
        pPrice.text = kDefaultContent;
        pPrice.textAlignment = NSTextAlignmentCenter;
        pPrice.adjustsFontSizeToFitWidth = YES;
        pNum.text = kDefaultContent;
        pNum.textAlignment = NSTextAlignmentRight;
        pNum.adjustsFontSizeToFitWidth = YES;
        pBuySell.textColor = pNum.textColor = kMYHexToRGB(0x858585, 1);
        pBuySell.font = pPrice.font = pNum.font = [UIFont systemFontOfSize:10];
        [self addSubview:pBuySell];
        [self addSubview:pPrice];
        [self addSubview:pNum];
        [self.pBuySellArray addObject:pBuySell];
        [self.pPriceArray addObject:pPrice];
        [self.pNumArray addObject:pNum];
    }
}

#pragma mark -
#pragma mark public
- (void)updateNewData:(HZMinPriceModel *)data
{
    NSArray *pTempArray = data.model_wuDangArray;
    if (!pTempArray || pTempArray.count == 0) {
        return;
    }
    int nCouts = (int)pTempArray.count;
    _nMaxNum = 0;
    
    for (HZWuDangModel *model in pTempArray) {
        NSInteger nNum = [model.model_num integerValue];
        _nMaxNum = MAX(_nMaxNum, nNum);
    }
    if (_nMaxNum == 0) {
        return;
    }
    for (int i = 0; i < nCouts; i++) {
        HZWuDangModel *wudangModel = pTempArray[i];
        wudangModel.model_prePrice = data.model_prePrice;
        UILabel *priceLabel = _pPriceArray[i];
        UILabel *numLabel = _pNumArray[i];
        priceLabel.text = wudangModel.model_price;
        numLabel.text = wudangModel.model_num;
        
        if ([wudangModel.model_price floatValue] > [wudangModel.model_prePrice floatValue]) {
            priceLabel.textColor = kREDColor;
        }else if ([wudangModel.model_price floatValue] < [wudangModel.model_prePrice floatValue]) {
            priceLabel.textColor = kGREENColor;
        }else {
            priceLabel.textColor = kGRAYColor;
        }
    }
    [self setNeedsLayout];
}

- (void)resetFont:(CGFloat)size
{
    [_pBuySellArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *buySell = obj;
        UILabel *price = _pPriceArray[idx];
        UILabel *num = _pNumArray[idx];
        buySell.font = price.font = num.font = [UIFont systemFontOfSize:size];
    }];
}
- (void)setVerticalLineColor:(UIColor *)color
{
    _pVerticalLine.backgroundColor = color;
}

- (void)clearData
{
    [_pPriceArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *price = obj;
        UILabel *num = _pNumArray[idx];
        price.text = kDefaultContent;
        num.text = kDefaultContent;
    }];
}

@end
